#import "ALTemplateEditorElementColumnEntry.h"
#import "SMRShadowTextField.h"


static const NSInteger ALEditTemplateColumnEntryLabelTag = 1;
static NSString* const ALTemplateEditorElementColumnEntryLabelFontContext = @"ALTemplateEditorElementColumnEntryLabelFontContext";
static NSColor *ALTemplateEditorElementColumnEntryLabelEditColor;


@interface ALTemplateEditorElementColumnEntry ()
@property (nonatomic, retain) NSView *infoView;
@property (nonatomic, assign) CGFloat initialViewHeight;
@end


@implementation ALTemplateEditorElementColumnEntry

@synthesize isDisplayingInfoView;
@synthesize infoView;
@synthesize initialViewHeight;


+(void)initialize {
	ALTemplateEditorElementColumnEntryLabelEditColor = [NSColor blackColor];
}

-(void)setView:(NSView *)view {
	[super setView:view];
	[self setInitialViewHeight:[view frame].size.height];
	SMRShadowTextField *label = (SMRShadowTextField *)[view viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[label setDelegate:self];
	[label addObserver:self forKeyPath:NSFontBinding options:NSKeyValueObservingOptionNew context:ALTemplateEditorElementColumnEntryLabelFontContext];
	[label bind:@"normalTextColor" toObject:self withKeyPath:@"elementLabelTextColor" options:nil];
//	[label setEditingTextColor:ALTemplateEditorElementColumnEntryLabelEditColor];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
	if (context != ALTemplateEditorElementColumnEntryLabelFontContext) {
		[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
		return;
	}
	
	// NSFontBinding for label text field
	SMRShadowTextField *label = (SMRShadowTextField *)[[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	NSSize minSize = [[label cell] cellSize];
	NSRect labelFrame = [label frame];
	labelFrame.size.height = minSize.height;
	CGFloat viewHeight = [[self view] frame].size.height;
	labelFrame.origin.y = (viewHeight - labelFrame.size.height) / 2;
	labelFrame = [[self view] centerScanRect:labelFrame];
	[label setFrame:labelFrame];
}

-(void)columnEntryDidBeginEditingAtPoint:(NSPoint)aPoint {
	[super columnEntryDidBeginEditingAtPoint:aPoint];
	SMRShadowTextField *label = (SMRShadowTextField *)[[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[label setEditable:YES];
	[label setNextKeyView:[self columnView]];
	[[label window] makeFirstResponder:label];
	[[label window] invalidateCursorRectsForView:label];
}

-(void)columnEntryDidEndEditing {
	SMRShadowTextField *label = (SMRShadowTextField *)[[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[label setEditable:NO];
}


#pragma mark -
#pragma mark Text delegate for label

-(BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command {
	SMRShadowTextField *label = [[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	if (control != label)
		return NO;
	if (command == @selector(cancelOperation:)) {
		[self discardEditing];
		NSWindow *controlWindow = [control window];
		[controlWindow makeFirstResponder:[self columnView]];
		return YES;
	}
	return NO;
}

- (void)controlTextDidEndEditing:(NSNotification *)aNotification {
	SMRShadowTextField *label = [[self view] viewWithTag:ALEditTemplateColumnEntryLabelTag];
	[[label cell] endEditing:nil];
	[[self columnView] stopEditing];
	[[label window] makeFirstResponder:[self columnView]];
}


#pragma mark -
#pragma mark Info view

-(void)hideInfoView {
	[self setIsDisplayingInfoView:NO];
	[[self infoView] removeFromSuperview];
	[self setMinimumHeight:[self initialViewHeight]];
	[[self parentEntry] noteHeightChangedForChildEntry:self];
}

-(void)displayInfoView:(NSView *)view {
	[self setIsDisplayingInfoView:YES];
	[self setInfoView:view];
	CGFloat infoViewHeight = [view frame].size.height;
	[self setMinimumHeight:infoViewHeight + [self initialViewHeight]];
	NSRect infoViewRect = NSMakeRect(0.0f, -infoViewHeight, [[self view] frame].size.width, infoViewHeight);
//	[[self view] addSubview:view];
	NSView * viewClone = [self cloneAndAdoptSubview:view];
	[viewClone setFrame:infoViewRect];
	[[self parentEntry] noteHeightChangedForChildEntry:self];
}


@end
